package hku.cs.lunuo.rank;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class RankMain2 {
	public static void main(String[] args) {

	}

	public void doMain(File testDir) throws JDOMException, IOException {
		SAXBuilder sb = new SAXBuilder();
		Document xmlDoc = sb.build(new File(testDir, "construct.xml"));
		Element xmlRoot = (Element) xmlDoc.getRootElement();
		Element xmlS = xmlRoot.getChild("testsuite");
		if (String.valueOf(Boolean.FALSE).equals(
				xmlS.getAttributeValue("status"))) {
			singleSuite(xmlS);
		} else {
			report(true);
		}

	}

	private void report(boolean b) {
		// TODO it is a successful report

	}

	public void singleSuite(Element suite) {
		Iterator xmlIt = suite.getChildren("testsuite").iterator();
		while (xmlIt.hasNext()) {
			Element xmlS = (Element) xmlIt.next();
			if (String.valueOf(Boolean.FALSE).equals(
					xmlS.getAttributeValue("status"))) {
				singleSuite(xmlS);
			}
		}

		xmlIt = suite.getChildren("testcase").iterator();
		while (xmlIt.hasNext()) {
			Element xmlS = (Element) xmlIt.next();
			if (String.valueOf(Boolean.FALSE).equals(
					xmlS.getAttributeValue("status"))) {
				singleTestcase(xmlS);
			}
		}
	}

	private void singleTestcase(Element testcase) {
//		HashMap<String, Boolean> result = new HashMap<String, Boolean>();
//		Set<String> failures = new HashSet<String>();
//		Iterator xmlIt = testcase.getChildren().iterator();
//		while (xmlIt.hasNext()) {
//			Element xmlM = (Element) xmlIt.next();
//			Boolean isS = Boolean.valueOf(xmlM
//					.getAttributeValue("status"));
//			String name = xmlM.getAttributeValue("name");
//			result.put(name, isS);
//			if(!isS)
//				failures.add(name.split("\\.")[0]);
//		}
//		List<String> failSet = new ArrayList<String>();
//		for(String key : failures) {
//			failSet.clear();
//			int i = 1;
//			failSet.add(key);
//			String name = key + "." + i;
//			while(result.containsKey(name)) {
//				failSet.add(name);
//				i++;
//			}
//			
//			
//					
//		}
		
		

	}
}
